﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PathSearcher
{
    class Step2
    {
        TimeTable timeTable = new TimeTable();

        public Step2()
        {
  
        }

        class Transit
        {
            public int StationID { get; set; }
            public int LineWhichTakeYouHere { get; set; }
            public int Time { get; set; }
        }

        public void GetTimes(List<List<NodeInfo>> solutions, int time)
        {


            foreach (List<NodeInfo> alternative in solutions)
            {
                if (alternative.Find(x=>x.StationId==16) !=null)
                {

                }

                Line tempResult = new Line();
                List<Transit> transits = new List<Transit>();
                bool valid = true;
                int tempTime = time;
                for (int i=0; i<alternative.Count;i++)
                {
                    Line line = null;
                    if ((i + 1) < alternative.Count)
                    {
                        line = timeTable.GetLineTime(alternative[i].StationId, tempTime, "n", 29, alternative[i + 1].ArrivalLines.ConvertAll(x => x.LineID))[0];
                        if (line == null)
                        {
                            //TODO: Break for 
                            valid = false;
                        }
                        else
                        {
                            tempTime = line.Time + FindDistance(alternative, line.LineID);
                            //tempResult.Add(line);
                        }
                    }

                    Transit transit = new Transit();
                    transit.LineWhichTakeYouHere = line != null ? line.LineID : -1;
                    transit.StationID = alternative[i].StationId;
                    transit.Time = line != null ? line.Time : -1;

                    transits.Add(transit);
                    
                }

                if (valid == true)
                {
                    if (transits.Find(x => x.StationID == 16) != null)
                    {

                    }

                }
            }



        }

        public int FindDistance(List<NodeInfo> nodes, int lineId)
        {
            foreach (NodeInfo node in nodes)
            {
                foreach (Line lineNode in node.ArrivalLines)
                {
                    if (lineNode.LineID == lineId)
                    {
                        return lineNode.Distance;
                    }
                }
            }

            throw new Exception(); 
        }
    }
}
